﻿using MaterialDesignColors.WpfExample.Domain;
using MaterialDesignThemes.Wpf;
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp1.Report;

namespace WpfApp1
{
    /// <summary>
    /// OrderDetailWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OrderDetailWindow : Window
    {
        public OrderDetailWindow()
        {
            InitializeComponent();
        }

        
        public int tt { get; set; }
        public string id { get; set; }
        public string lang { get; set; }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            setlang();
            loadGrid();

        }

        SQLiteHelper sqlite = new SQLiteHelper();

        private void tip(int type, string msg)
        {

            if (type == 1)
            {
                MainSnackbar.MessageQueue.Enqueue(msg);
            }
            

        }


        private void PrintDetails(string orderid,bool one)
        {
            string report_wenjianming = "Report2.rdlc";

            if (one)
            {

                DataTable order_detail = sqlite.getData("select * from 'order_detail_view' where id='" + orderid + "'   and lang='" + lang + "' ");

                if (order_detail.Rows.Count == 0)
                {
                    return;
                }

                string food_type=order_detail.Rows[0]["food_id"].ToString();
 
                DataTable dt2 = sqlite.getData("select * from 'printer_binding_view' where food_type='" + food_type + "' and lang='" + lang + "' ");

                if (dt2.Rows.Count == 0)
                {
                    tip(1, "بۇ تاماق تۈرىگە پىرىنتېر باغلانمىغان");
                    return;
                }

                string printerName = dt2.Rows[0]["printer_name"].ToString();
                string printCount1 = dt2.Rows[0]["print_count"].ToString();
                string paper_width1 = dt2.Rows[0]["paper_width"].ToString();
                string paper_height1 = dt2.Rows[0]["paper_height"].ToString();

                List<Orders> orders = new List<Orders>();

                string table_name = "";
                string description = "";
                string order_number = "";
                string time = "";
                for (int j = 0; j < order_detail.Rows.Count; j++)
                {

                    List<Orders> orders2_1 = new List<Orders>();

                    string detail_id = order_detail.Rows[j]["id"].ToString();

                    string food_name_ug = order_detail.Rows[j]["food_name"].ToString();
                    string food_price = order_detail.Rows[j]["price"].ToString();

                    table_name = order_detail.Rows[j]["table_name"].ToString();
                    description = order_detail.Rows[j]["description"].ToString();
                    order_number = order_detail.Rows[j]["order_number"].ToString();
                    time = order_detail.Rows[j]["created_at"].ToString();

                    string food_number = order_detail.Rows[j]["number"].ToString();

                    int ct = 0;
                    int.TryParse(food_number, out ct);

                    Orders orders1 = new Orders(food_name_ug, food_price, ct, table_name);

                    orders2_1.Add(orders1);




                    List<ReportParameter> pr1_1 = new List<ReportParameter>();

                    pr1_1.Add(new ReportParameter("order_number", order_number, true));
                    pr1_1.Add(new ReportParameter("table_name", table_name, true));
                    pr1_1.Add(new ReportParameter("description", description, true));
                    pr1_1.Add(new ReportParameter("time", time, true));

                    

                    string path1 = Environment.CurrentDirectory;

                    LocalReport localReport1 = new LocalReport();

                    localReport1.ReportPath = path1 + @"\Report\" + report_wenjianming;



                    int width1 = 0;
                    int.TryParse(paper_width1, out width1);
                    if (width1 <= 0)
                    {
                        return;
                    }

                    int height1 = 0;
                    int.TryParse(paper_height1, out height1);
                    if (height1 <= 0)
                    {
                        return;
                    }

                    //  for (int index = 0; index < cct; index++)
                    //  {
                    RDLCPrint aa1 = new RDLCPrint(true,false);
                    aa1.isDetail1 = true;
                    aa1.Run(localReport1, "DataSet1", orders2_1, pr1_1, printerName, detail_id, printCount1, width1, height1);  //每个饭菜都需要打印一次


                    orders.Add(orders1);
                }



            }
            else
            {

                List<Orders> orders = new List<Orders>();

                 report_wenjianming = "Report1.rdlc";

               DataRow order_dt=sqlite.getDataRow("select * from 'order' where id='"+orderid+"'");
                if (order_dt==null)
                {
                    return;
                }

                string id = order_dt["id"].ToString();
                string order_number = order_dt["order_number"].ToString();
                string table_name = order_dt["table_name"].ToString();
                string created_at = order_dt["created_at"].ToString();
                string price = order_dt["price"].ToString();
                string description = order_dt["description"].ToString();
                

                DataTable order_detail = sqlite.getData("select * from 'order_detail_view' where order_id='" + orderid + "' and lang='" + lang + "' ");

                for (int j = 0; j < order_detail.Rows.Count; j++)
                {
                    string food_name_ug = order_detail.Rows[j]["food_name"].ToString();
                    string food_price = order_detail.Rows[j]["price"].ToString();
                    string food_number = order_detail.Rows[j]["number"].ToString();

                    int ct = 0;
                    int.TryParse(food_number, out ct);

                    Orders orders1 = new Orders(food_name_ug, food_price, ct, table_name);

                    orders.Add(orders1);
                }

                List<ReportParameter> pr1 = new List<ReportParameter>();
                string resturant_name = sqlite.getValue("select value from sys_config where name='resturant_name' and lang='" + lang + "'", "value");

                pr1.Add(new ReportParameter("resturant_name", resturant_name, true));
                pr1.Add(new ReportParameter("table_name", table_name, true));
                pr1.Add(new ReportParameter("order_number", order_number, true));
                pr1.Add(new ReportParameter("time", created_at, true));
                pr1.Add(new ReportParameter("price", price, true));
                pr1.Add(new ReportParameter("description", description, true));



                string path = Environment.CurrentDirectory;

                LocalReport localReport = new LocalReport();
                localReport.ReportPath = path + @"\Report\" + report_wenjianming;

                //  string printCount = sqlite.getValue("select print_count from printer where print_type='1' and status='1' and lang='" + lang + "' ", "print_count");

                string sql = "select * from printer where print_type='1'  and lang='" + lang + "' ";
                DataRow drow = sqlite.getDataRow(sql);
                if (drow != null)
                {
                    string printerName = drow["device_name"].ToString();

                    string printCount = drow["print_count"].ToString();

                    string paper_width_txt = drow["paper_width"].ToString();

                    string paper_height_txt = drow["paper_height"].ToString();


                    int width = 0;
                    int.TryParse(paper_width_txt, out width);
                    if (width <= 0)
                    {
                        return;
                    }

                    int height = 0;
                    int.TryParse(paper_height_txt, out height);
                    if (height <= 0)
                    {
                        return;
                    }


                    RDLCPrint aa = new RDLCPrint(false, false);
                    aa.isDetail1 = false;
                    aa.Run(localReport, "DataSet1", orders, pr1, printerName, id, printCount, width, height);
                   

                }
                DataTable detail_dt = sqlite.getData("select * from 'order_detail' where order_id='" + id + "'");
                for (int i = 0; i < detail_dt.Rows.Count; i++)
                {
                    string detail_id = detail_dt.Rows[i]["id"].ToString();
                    PrintDetails(detail_id, true);
                }




            }

        }


        private void PrintDetails2(string orderid,bool one)
        {
            if (one)
            {


                string sql = "select DISTINCT food_id  from 'order_out_detail_view' where id='" + orderid + "' and lang='" + lang + "'";
               

                DataTable dt1 = sqlite.getData(sql);

                string report_wenjianming = "Report4.rdlc";



                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    string food_type = dt1.Rows[i]["food_id"].ToString();


                    DataTable dt2 = sqlite.getData("select * from 'printer_binding_view' where food_type='"
                      + food_type + "' and lang='" + lang + "' ");

                    if (dt2.Rows.Count == 0)
                    {
                        tip(1, "بۇ تاماق تۈرىگە پىرىنتېر باغلانمىغان");
                        return;
                    }

                    string printerName = dt2.Rows[0]["printer_name"].ToString();
                    string printCount1 = dt2.Rows[0]["print_count"].ToString();
                    string paper_width1 = dt2.Rows[0]["paper_width"].ToString();
                    string paper_height1 = dt2.Rows[0]["paper_height"].ToString();


                    DataTable order_detail = sqlite.getData("select * from 'order_out_detail_view' where id='"
                                            + orderid + "' and food_id = '" + food_type + "'  and lang='" + lang + "' ");



                    string table_name = "";
                    string description = "";
                    string order_number = "";
                    for (int j = 0; j < order_detail.Rows.Count; j++)
                    {

                        List<Orders> orders = new List<Orders>();


                        string detail_id = order_detail.Rows[j]["id"].ToString();

                        string food_name_ug = order_detail.Rows[j]["food_name"].ToString();
                        string food_price = order_detail.Rows[j]["price"].ToString();

                        description = order_detail.Rows[j]["description"].ToString();
                        order_number = order_detail.Rows[j]["order_number"].ToString();

                        string food_number = order_detail.Rows[j]["number"].ToString();

                        int ct = 0;
                        int.TryParse(food_number, out ct);

                        Orders orders1 = new Orders(food_name_ug, food_price, ct, table_name);

                        orders.Add(orders1);



                        List<ReportParameter> pr1 = new List<ReportParameter>();

                        string time = sqlite.getValue("select delivery_time from 'order_out' where id='" + orderid + "' and lang='" + lang + "' ", "delivery_time");

                        pr1.Add(new ReportParameter("order_number", order_number, true));
                        pr1.Add(new ReportParameter("time", time, true));
                        pr1.Add(new ReportParameter("description", description, true));

                       

                        string path = Environment.CurrentDirectory;

                        LocalReport localReport = new LocalReport();

                        localReport.ReportPath = path + @"\Report\" + report_wenjianming;

                        string printCount = dt2.Rows[i]["print_count"].ToString();
                        string paper_width = dt2.Rows[i]["paper_width"].ToString();
                        string paper_height = dt2.Rows[i]["paper_height"].ToString();

                        int width = 0;
                        int.TryParse(paper_width, out width);
                        if (width <= 0)
                        {
                            return;
                        }

                        int height = 0;
                        int.TryParse(paper_height, out height);
                        if (height <= 0)
                        {
                            return;
                        }

                        RDLCPrint aa = new RDLCPrint(false,false);
                        aa.isDetail2 = false;

                        aa.Run2(localReport, "DataSet1", orders, pr1, printerName, orderid, printCount, width, height);


                    }
                }





            }
            else
            {

                string sql = "select DISTINCT food_id  from 'order_out_detail_view' where order_id='" + orderid + "' and lang='" + lang + "'";
                

                DataTable dt1 = sqlite.getData(sql);

                string report_wenjianming = "Report4.rdlc";



                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    string food_type = dt1.Rows[i]["food_id"].ToString();


                    DataTable dt2 = sqlite.getData("select * from 'printer_binding_view' where food_type='"
                      + food_type + "' and lang='" + lang + "' ");

                    if (dt2.Rows.Count == 0)
                    {
                       // tip(1, "بۇ تاماق تۈرىگە پىرىنتېر باغلانمىغان");
                        return;
                    }

                    string printerName = dt2.Rows[0]["printer_name"].ToString();
                    string printCount1 = dt2.Rows[0]["print_count"].ToString();
                    string paper_width1 = dt2.Rows[0]["paper_width"].ToString();
                    string paper_height1 = dt2.Rows[0]["paper_height"].ToString();


                    DataTable order_detail = sqlite.getData("select * from 'order_out_detail_view' where order_id='"
                                            + orderid + "' and food_id = '" + food_type + "'  and lang='" + lang + "' ");




                    List<Orders> orders = new List<Orders>();

                    string table_name = "";
                    string description = "";
                    string order_number = "";
                    for (int j = 0; j < order_detail.Rows.Count; j++)
                    {

                       

                        string detail_id = order_detail.Rows[j]["id"].ToString();

                        string food_name_ug = order_detail.Rows[j]["food_name"].ToString();
                        string food_price = order_detail.Rows[j]["price"].ToString();

                        description = order_detail.Rows[j]["description"].ToString();
                        order_number = order_detail.Rows[j]["order_number"].ToString();

                        string food_number = order_detail.Rows[j]["number"].ToString();

                        int ct = 0;
                        int.TryParse(food_number, out ct);

                        Orders orders1 = new Orders(food_name_ug, food_price, ct, table_name);

                        orders.Add(orders1);



                       

                    }

                    List<ReportParameter> pr1 = new List<ReportParameter>();

                    string time = sqlite.getValue("select delivery_time from 'order_out' where id='" + orderid + "' and lang='" + lang + "' ", "delivery_time");

                    pr1.Add(new ReportParameter("order_number", order_number, true));
                    pr1.Add(new ReportParameter("time", time, true));
                    pr1.Add(new ReportParameter("description", description, true));

                    if (string.IsNullOrEmpty(printerName))
                    {
                        // MessageBox.Show("no printer found!");
                        break;
                    }

                    string path = Environment.CurrentDirectory;

                    LocalReport localReport = new LocalReport();

                    localReport.ReportPath = path + @"\Report\" + report_wenjianming;

                    string printCount = dt2.Rows[i]["print_count"].ToString();
                    string paper_width = dt2.Rows[i]["paper_width"].ToString();
                    string paper_height = dt2.Rows[i]["paper_height"].ToString();

                    int width = 0;
                    int.TryParse(paper_width, out width);
                    if (width <= 0)
                    {
                        return;
                    }

                    int height = 0;
                    int.TryParse(paper_height, out height);
                    if (height <= 0)
                    {
                        return;
                    }

                    RDLCPrint aa = new RDLCPrint(false, false);
                    aa.isDetail2 = false;

                    aa.Run2(localReport, "DataSet1", orders, pr1, printerName, orderid, printCount, width, height);



                }


            }

        }



        private void setlang()
        {
            if (lang.Equals("ug"))
            {
                this.FlowDirection = FlowDirection.RightToLeft;
                col_food_name.Header = "ئىسمى";
                col_price.Header = "باھاسى";
                col_description.Header = "ئىزاھات";
                col_number.Header = "سانى";
                btn_print.Content = "بېسىش";
                btn_close.Content = "چېكىنىش";
                
            }
            else
            {
                this.FlowDirection = FlowDirection.LeftToRight;
                col_food_name.Header = "名称";
                col_price.Header = "价格";

                col_description.Header = "注释";
                col_number.Header = "数量";
                btn_print.Content = "打印";
                btn_close.Content = "退出";

            }
        }

        private void loadGrid()
        {
            SQLiteHelper sqlite = new SQLiteHelper();
            string sql = "";
            
            if (tt > 0)
            {
                sql = "select  * from order_out_detail_view where lang='" + lang + "' and order_id='" + id + "'";
            }
            else
            {
                sql = "select  * from order_detail_view where lang='" + lang + "' and order_id='" + id + "'";
            }

            DataTable data = sqlite.getData(sql);
            dgv1.ItemsSource = data.DefaultView;
           
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;
            switch (name)
            {
                case "btn_print":


                    if (tt > 0)
                    {
                        PrintDetails2(id,false);
                    }
                    else
                    {
             
                    PrintDetails(id,false);
                    }
                    break;
                case "btn_close":

                    this.Close();

                    break;
                case "order_btn_print1":



                    if (dgv1.SelectedItem != null)
                    {
                        DataRowView row = (DataRowView)dgv1.SelectedItem;
                        string detail_id = row.Row.ItemArray[row.Row.ItemArray.Count()-1].ToString();

                       
                        if (tt > 0)
                        {
                            PrintDetails2(detail_id,true);
                        }
                        else
                        {

                            PrintDetails(detail_id,true);
                        }

                    }

                        

                    break;
            }
        }
    }
}
